package ru.compft.common.data;

import ru.compft.common.data.meta.RoleToUserTableMeta;
import ru.compft.common.data.meta.UserTableMeta;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * User: ASUS
 * Date: 01.06.12
 * Time: 0:10
 * Класс представляющий объект "Пользователь системы"
 */
@Entity
@Table(name = UserTableMeta.TABLE_NAME)
public class AppUser implements Serializable {
    private static final long serialVersionUID = 8218570008824741548L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = UserTableMeta.ID, unique = true, nullable = false)
    private Long id;

    @Column(name = UserTableMeta.FIRST_NAME)
    private String firstName;

    @Column(name = UserTableMeta.LAST_NAME)
    private String lastName;

    @Column(name = UserTableMeta.LOGIN, unique = true, nullable = false)
    private String login;

    @Column(name = UserTableMeta.PASSWORD)
    private String password;

    @Column(name = UserTableMeta.ENABLED, length = 1)
    private boolean enabled;

    @Column(name = UserTableMeta.ACCOUNT_NON_EXPIRED, length = 1)
    private boolean accountNonExpired;

    @Column(name = UserTableMeta.CREDENTIALS_NON_EXPIRED, length = 1)
    private boolean credentialsNonExpired;

    @Column(name = UserTableMeta.ACCOUNT_NON_LOCKED, length = 1)
    private boolean accountNonLocked;

    @Column(name = UserTableMeta.REG_DATE, nullable = false)
    private Date regDate;

    @Column(name = UserTableMeta.LAST_ACTIVITY)
    private Date lastActivity;

    @Transient
    private List<AppGroup> groups; // членство в группах

    @ManyToMany(
            targetEntity = AppRole.class,
            cascade = {CascadeType.PERSIST, CascadeType.MERGE},
            fetch = FetchType.LAZY
    )
    @JoinTable(
            name = RoleToUserTableMeta.TABLE_NAME,
            joinColumns = @JoinColumn(name = RoleToUserTableMeta.USER_ID),
            inverseJoinColumns = @JoinColumn(name = RoleToUserTableMeta.ROLE_ID)
    )
    public Set<AppRole> roles; // роли пользователя

    public AppUser() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    public Date getRegDate() {
        return regDate;
    }

    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }

    public Date getLastActivity() {
        return lastActivity;
    }

    public void setLastActivity(Date lastActivity) {
        this.lastActivity = lastActivity;
    }

    public Set<AppRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<AppRole> roles) {
        this.roles = roles;
    }

    public void addRole(AppRole appRole) {
        if (appRole != null) {
            if (getRoles() == null)
                setRoles(new HashSet<AppRole>());

            getRoles().add(appRole);
        }
    }
}
